package me.jessyan.mvparms.demo.mvp.model.logic;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.net.wifi.WifiConfiguration;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.platform.ai.control.R;

import me.jessyan.mvparms.demo.mvp.ui.view.VolumePopWindow;
import me.jessyan.mvparms.demo.util.Utils;


/**
 * The author: Zhao X.H.
 * Creation time: 2018/2/5 ${time}
 */

public class AllDialogUtil {
    private PopupWindow popupWindow;
    private VolumePopWindow volumePop;
    private Dialog progressDialog;
    public static AllDialogUtil instance;
    public static AllDialogUtil getInstance(){
        if(instance==null){
            synchronized (AllDialogUtil.class){
                if(instance==null){
                    instance=new AllDialogUtil();
                }
            }
        }
        return instance;
    }

    private AllDialogUtil(){

    }

    public void showVolumePopWindow(Context mContext, View v){
        volumePop = new VolumePopWindow(mContext);
        volumePop.showPopupWindowByCenter(v);
    }


    private ProgressDialog waitDialog;
    public void initWaitDialog(Context context) {
        waitDialog = new ProgressDialog(context);
        waitDialog.setIndeterminate(false);//循环滚动
        waitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        waitDialog.setMessage("正在连接");
        waitDialog.setCancelable(true);//false不能取消显示，true可以取消显示
        waitDialog.show();
    }
    public void showWaitDialog() {
        waitDialog.show();
    }
    public void dismissWaitDialog() {
        waitDialog.dismiss();
    }


//    private void showPopupWindow(View rootview,String ssid) {
//        //设置contentView
//        View contentView = LayoutInflater.from(mContext).inflate(R.layout.pop_wifi, null);
//        PopupWindow mPopWindow = new PopupWindow(contentView,
//                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//        mPopWindow.setContentView(contentView);
//        //设置各个控件的点击响应et_wifi_name
//        EditText et_name = (EditText) contentView.findViewById(R.id.et_wifi_name);
//        EditText et_pwd = (EditText) contentView.findViewById(R.id.et_wifi_pwd);
//        et_name.setImeOptions(EditorInfo.IME_ACTION_DONE);
//        et_pwd.setImeOptions(EditorInfo.IME_ACTION_DONE);
//        et_name.setText(ssid);
//        et_name.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if(!TextUtil.isEmpty(v.getText().toString())){
//                    et_name.setText(v.getText());
//                }
//                Utils.closeKeybord(mContext,contentView);
//                return true ;
//            }
//        });
//        et_pwd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if(!TextUtil.isEmpty(v.getText().toString())){
//                    et_pwd.setText(v.getText());
//                }
//                Utils.closeKeybord(mContext,contentView);
//                return true ;
//            }
//        });
//        contentView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mPopWindow.dismiss();
//                mPopWindow.setContentView(contentView);//设置布局
//                WindowManager.LayoutParams lp = getWindow().getAttributes();
//                lp.alpha = 1f; //0.0-1.0
//                getWindow().setAttributes(lp);
//            }
//        });
//        contentView.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //按下确定键后的事件
//                //获取wifi开关状态
//                mWifiManager.setWifiEnabled(true);
//                WifiConfiguration config = createWifiConfig(et_name.getText().toString(),et_pwd.getText().toString(),WIFICIPHER_WPA);
//                int res = mWifiManager.addNetwork(config);
//                if(res != -1){
//                    Toast.makeText(mContext,"WIFI配置成功",Toast.LENGTH_SHORT).show();
//                    mWifiManager.enableNetwork(res,true);
//                    mWifiManager.saveConfiguration();
//                }else{
//                    Toast.makeText(mContext,"账号/密码错误",Toast.LENGTH_SHORT).show();
//                }
//                mPopWindow.dismiss();
//            }
//        });
//        mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                mPopWindow.dismiss();
//                mPopWindow.setContentView(contentView);//设置布局
//                WindowManager.LayoutParams lp = getWindow().getAttributes();
//                lp.alpha = 1f; //0.0-1.0
//                getWindow().setAttributes(lp);
//                Utils.closeKeybord(mContext,contentView);
//            }
//        });
//        mPopWindow.setContentView(contentView);//设置布局
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.alpha = 0.5f; //0.0-1.0
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        getWindow().setAttributes(lp);
//        //显示PopupWindow
//        mPopWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);
//    }
}
